<script setup>
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
import { getUserListService, updateStatusService } from "@/api/users";

const loading = ref(false);
const params = reactive({
  pageNum: 1,
  pageSize: 10,
  userId: "",
  username: "",
  nickName: "",
});
const userList = ref([]);
const total = ref(0);

// 获取用户列表
const getUserList = async () => {
  loading.value = true;
  try {
    const result = await getUserListService(params);
    console.log("API响应:", result);
    userList.value = result.rows;
    total.value = result.total;
  } catch (error) {
    console.error("获取用户列表失败:", error);
    ElMessage.error("获取用户列表失败");
  } finally {
    loading.value = false;
  }
};

// 初始化加载用户列表
getUserList();

// 分页处理
function handleSizeChange(newSize) {
  params.pageSize = newSize;
  params.pageNum = 1;
  getUserList();
}

function handleCurrentChange(newPage) {
  params.pageNum = newPage;
  getUserList();
}
// 拉黑/取消拉黑用户
const updateStatus = async (userId, status) => {
  // 拉黑/取消拉黑用户
  try {
    const result = await updateStatusService({
      userId,
      status,
    });
    console.log("API响应:", result);
    ElMessage.success("操作成功");
    getUserList();
  } catch (error) {
    console.error("操作失败:", error);
    ElMessage.error("操作失败");
  }
};

// 搜索用户
const handleSearch = () => {
  params.pageNum = 1;
  getUserList();
};

// 重置搜索条件
const handleReset = () => {
  // 重置表单
  Object.keys(params).forEach((key) => {
    if (key !== "pageNum" && key !== "pageSize") {
      params[key] = "";
    }
  });
  params.pageNum = 1;
  // 重新加载所有用户数据
  getUserList();
  ElMessage.success("搜索条件已重置");
};
</script>
<template>
  <div class="user-manage">
    <div class="header">
      <h2>用户管理</h2>
      <!-- <el-button type="primary" @click="handleAdd">添加用户</el-button> -->
    </div>

    <!-- 搜索表单区域 -->
    <div class="search-form">
      <el-form :inline="true">
        <el-form-item>
          <el-input
            v-model="params.userId"
            placeholder="请输入要搜索的用户ID"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="params.username"
            placeholder="请输入要搜索的用户邮箱"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="params.nickName"
            placeholder="请输入要搜索的用户昵称"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="userList" style="width: 100%" v-loading="loading">
      <el-table-column prop="userId" label="用户ID" width="100" />
      <el-table-column prop="nickName" label="用户昵称" width="120" />
      <el-table-column prop="phone" label="手机号" width="120" />
      <el-table-column prop="email" label="邮箱" width="180" />
      <el-table-column prop="wechat" label="微信" width="120" />
      <el-table-column label="学校/专业" width="180">
        <template #default="{ row }">
          {{ row.schoolName }} / {{ row.majorName }}
        </template>
      </el-table-column>
      <el-table-column prop="bio" label="个人介绍" />
      <el-table-column prop="status" label="用户状态" width="80">
        <template #default="{ row }">
          <el-tag :type="row.status === 1 ? 'success' : 'info'">
            {{ row.status === 1 ? "正常" : "拉黑" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="{ row }">
          <el-button
            v-if="row.status === 1"
            type="danger"
            link
            @click="updateStatus(row.userId, 0)"
            class="action-btn"
            >拉黑</el-button
          >
          <el-button
            v-else
            type="success"
            link
            @click="updateStatus(row.userId, 1)"
            class="action-btn"
            >取消拉黑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        size="small"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        v-model:current-page="params.pageNum"
        v-model:page-size="params.pageSize"
        :page-sizes="[1, 5, 10, 15, 20]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.user-manage {
  padding: 20px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    h2 {
      margin: 0;
      color: #2a6fc7;
    }
  }

  .search-form {
    margin-bottom: 16px;
    padding: 16px;
    background-color: #f0f2f5;
    border-radius: 6px;

    .el-form {
      margin-bottom: 0;
    }

    .el-form-item {
      margin-right: 16px;
      margin-bottom: 0;
    }

    .el-input__wrapper {
      border-radius: 4px;
    }
  }

  :deep(.el-table) {
    --el-table-header-bg-color: #2a6fc7;
    --el-table-header-text-color: #ffffff;
    --el-table-border-color: #e4e7ed;

    .el-table__header-wrapper {
      .el-table__header {
        th {
          font-weight: normal;
          background-color: #2a6fc7;
          color: #ffffff;
        }
      }
    }

    .el-table__body {
      tr {
        height: 48px;
      }

      td {
        padding: 8px 0;
        color: #303133;
      }
    }

    .action-btn {
      margin-right: 8px;

      &:last-child {
        margin-right: 0;
      }
    }
  }

  .pagination-container {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
  }
}
</style>
